cmake_minimum_required(VERSION 3.10)
project(case21)

add_executable(main main.cpp)

add_subdirectory(sub1)
add_subdirectory(sub2)

get_directory_property(TARGET_LIST BUILDSYSTEM_TARGETS)

function (_get_all_cmake_targets out_var current_dir)
    get_property(targets DIRECTORY ${current_dir} PROPERTY BUILDSYSTEM_TARGETS)
    get_property(subdirs DIRECTORY ${current_dir} PROPERTY SUBDIRECTORIES)

    foreach(subdir ${subdirs})
        _get_all_cmake_targets(subdir_targets ${subdir})
        list(APPEND targets ${subdir_targets})
    endforeach()

    set(${out_var} ${targets} PARENT_SCOPE)
endfunction()

# Run at end of top-level CMakeLists
_get_all_cmake_targets(all_targets ${CMAKE_CURRENT_LIST_DIR})

foreach(TARGET_VAR ${all_targets})
    message(STATUS target: ${TARGET_VAR})
endforeach()
